package com.easypan.service.impl;

import ch.qos.logback.classic.Logger;
import com.easypan.entity.constants.Constants;
import com.easypan.service.FileProcessingStrategy;
import com.easypan.utils.ScaleFilter;
import com.easypan.utils.StringTools;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;



/**
 * @author cyb
 * @create 2024-12-17-19:08
 * @description 实现具体策略类
 */
public class ImageProcessingStrategy implements FileProcessingStrategy {

    @Override
    public void processFile(String fileId, String targetFilePath, String month, String currentUserFolderName, String targetFolderName) {
        // 图片文件生成缩略图，缩略图改名字_
        String realFileName = currentUserFolderName + StringTools.getFileSuffix(targetFilePath);
        String cover = month + "/" + realFileName.replace(".", "_.");
        String coverPath = targetFolderName + "/" + cover;

        Boolean created = ScaleFilter.createThumbnailWidthFFmpeg(new File(targetFilePath), Constants.LENGTH_150, new File(coverPath), false);
        if (!created) {
            // 直接copy一份
            try {
                FileUtils.copyFile(new File(targetFilePath), new File(coverPath));
            } catch (IOException e) {
                Logger logger = null;
                logger.error("Failed to copy file as thumbnail", e);
            }
        }
    }
}